﻿#include <QTimerEvent>
#include <QEventLoop>
#include <QTimer>
#include "CWorkMng.h"
#include "CSqlOper.h"
#include "CGlobal.h"
#include "CLogOper.h"
#include "Public.h"

CWorkMng::CWorkMng(CCamerCtrMange* pCamerCtrMange, CDevDataBeltRealign *pDevDataBeltRealign)
	:m_pCamerCtrMange(pCamerCtrMange)
{
	m_pDevDataBeltRealign = pDevDataBeltRealign;
	m_nResultCount = 0;
	m_bIsRecord = false;
	m_sLastGuid = "";
	m_pMutex = new QMutex(QMutex::Recursive);
	m_bhread.store(false);
	m_bShouldStop = false;
	m_bFrist = true;
	m_nLoginCount = 10;
}

CWorkMng::~CWorkMng()
{
	// 1. 先设置停止标志
	m_bShouldStop = true;

	// 2. 停止工作线程
	threadStop();

	// 3. 安全停止线程
	if (isRunning()) 
	{
		quit();
		if (!wait(3000)) 
		{ // 等待3秒超时
			terminate();
			wait(1000);
		}
	}

	// 4. 清理资源（此时线程已完全停止）
	if (m_pMutex != nullptr)
	{
		delete m_pMutex;
		m_pMutex = nullptr;
	}
}

void CWorkMng::threadStart()
{
	QMutexLocker locker(m_pMutex);
	if (!m_bhread.load())
	{
		if (m_bFrist)
		{
			start();
			m_bFrist = false;
		}
		m_bhread.store(true);
	}
}

void CWorkMng::threadStop()
{
	QMutexLocker locker(m_pMutex);
	if (m_bhread.load())
	{
		m_bhread.store(false);
	}
}

void CWorkMng::updateBeltCorrectState()
{
	// 检查指针有效性
	if (!m_pDevDataBeltRealign || m_bShouldStop) 
	{
		return;
	}
	//首先查询皮带运行信号
	QVector<QSqlRecord> vecRecord;
	QMap<QString, int> mapBeltState;
	QString sErrMsg = "";
	CSqlOper::instance().selectBeltWork(vecRecord, sErrMsg);
	for (int i = 0; i < vecRecord.size(); i++)
	{
		mapBeltState.insert(vecRecord[i].value("GUID").toString(), vecRecord[i].value("TAG_VALUE").toInt());
	}

	if (CSqlOper::instance().selectBeltCorrect(m_pDevDataBeltRealign->m_sPointGuid, vecRecord, sErrMsg))
	{
		for (int i = 0; i < vecRecord.size(); i++)
		{
			m_pDevDataBeltRealign->m_pMutex->lock();
			// 检查设备数据指针有效性
			if (!m_pDevDataBeltRealign->m_pDevDataCam)
			{
				continue;
			}
			m_pDevDataBeltRealign->m_sBeltStateGuid = vecRecord[i].value("BELTSTATE_GUID").toString();
			m_pDevDataBeltRealign->m_sRecordPath = vecRecord[i].value("PIC_PATH").toString();
			m_pDevDataBeltRealign->m_dLeftRaito = vecRecord[i].value("LEFT_RATIO").toDouble();
			m_pDevDataBeltRealign->m_dRightRaito = vecRecord[i].value("RIGHT_RATIO").toDouble();
			m_pDevDataBeltRealign->m_dLeftCurrVal = vecRecord[i].value("LEFT_CURREENT_VALUE").toDouble() / 1000 * m_pDevDataBeltRealign->m_dLeftRaito;
			m_pDevDataBeltRealign->m_dRightCurrVal = vecRecord[i].value("RIGHT_CURREENT_VALUE").toDouble() / 1000 * m_pDevDataBeltRealign->m_dRightRaito;
			m_pDevDataBeltRealign->m_pDevDataCam->m_dLeftRectifyValue = vecRecord[i].value("LEFT_CURREENT_VALUE").toDouble() / 1000 * m_pDevDataBeltRealign->m_dLeftRaito;
			m_pDevDataBeltRealign->m_pDevDataCam->m_dRightRectifyValue = vecRecord[i].value("RIGHT_CURREENT_VALUE").toDouble() / 1000 * m_pDevDataBeltRealign->m_dRightRaito;
			m_pDevDataBeltRealign->m_nRealignSetValue = vecRecord[i].value("REALIGN_SET_VALUE").toInt();
			m_pDevDataBeltRealign->m_nRealignSetValueTwo = vecRecord[i].value("REALIGN_SET_VALUE_TWO").toInt();
			m_pDevDataBeltRealign->m_pDevDataCam->m_nRectifySetValue = vecRecord[i].value("REALIGN_SET_VALUE").toInt();
			m_pDevDataBeltRealign->m_pDevDataCam->m_nRectifySetValueTwo = vecRecord[i].value("REALIGN_SET_VALUE_TWO").toInt();
			m_pDevDataBeltRealign->m_pDevDataCam->m_nIsHaveBeltState = m_pDevDataBeltRealign->m_nIsHaveBeltRunState;
			m_pDevDataBeltRealign->m_pDevDataCam->m_bBeltState = m_pDevDataBeltRealign->m_bBeltWork;
			//判断算法在线状态
			int nAlgNum = vecRecord[i].value("PLC_HEART").toInt();
			if (m_pDevDataBeltRealign->m_nAlgTime == 120)
			{
				if (nAlgNum != m_pDevDataBeltRealign->m_nAlgNum)
				{
					m_pDevDataBeltRealign->m_nAlgState = 1;
					m_pDevDataBeltRealign->m_nAlgNum = nAlgNum;
				}
				else
				{
					m_pDevDataBeltRealign->m_nAlgState = 0;
				}
				m_pDevDataBeltRealign->m_nAlgTime = 0;
			}
			else
			{
				m_pDevDataBeltRealign->m_nAlgTime++;
			}
			QString sBeltStateGuid = m_pDevDataBeltRealign->m_sBeltStateGuid;
			int nBeltStateType = m_pDevDataBeltRealign->m_nBeltStateType;
			if (m_pDevDataBeltRealign->m_mapInputPoint[A2T("皮带运行信号")] != nullptr)
			{
				m_pDevDataBeltRealign->m_bBeltWork = m_pDevDataBeltRealign->m_mapInputPoint[A2T("皮带运行信号")]->getValue().toBool();
			}
			m_pDevDataBeltRealign->m_pMutex->unlock();

			m_pDevDataBeltRealign->m_pMutex->lock();
			//跑偏状态
			if (m_pDevDataBeltRealign->m_dLeftCurrVal * 10 <= m_pDevDataBeltRealign->m_nRealignSetValueTwo ||
				m_pDevDataBeltRealign->m_dRightCurrVal * 10 <= m_pDevDataBeltRealign->m_nRealignSetValueTwo)//二级跑偏
			{
				m_pDevDataBeltRealign->m_bIsCorrectTwo = true;
				m_pDevDataBeltRealign->m_bIsCorrect = false;
			}
			else if ((m_pDevDataBeltRealign->m_dLeftCurrVal*10 <= m_pDevDataBeltRealign->m_nRealignSetValue && m_pDevDataBeltRealign->m_dLeftCurrVal * 10 > m_pDevDataBeltRealign->m_nRealignSetValueTwo)
				|| (m_pDevDataBeltRealign->m_dRightCurrVal * 10 <= m_pDevDataBeltRealign->m_nRealignSetValue && m_pDevDataBeltRealign->m_dRightCurrVal * 10 > m_pDevDataBeltRealign->m_nRealignSetValueTwo))//一级跑偏
			{
				m_pDevDataBeltRealign->m_bIsCorrectTwo = false;
				m_pDevDataBeltRealign->m_bIsCorrect = true;
			}
			else
			{
				m_pDevDataBeltRealign->m_bIsCorrect = false;
				m_pDevDataBeltRealign->m_bIsCorrectTwo = false;
			}
			QString strValueTemp = vecRecord[i].value("BELT_OUTLINE_COORD").toString();
			//先判断是否包含左右括号
			if (strValueTemp.contains("(") && strValueTemp.contains(")"))
			{
				strValueTemp.remove("(");
				strValueTemp.remove(")");

				//使用分号(;)区分4个坐标
				QList<QString> listCoord;
				listCoord = strValueTemp.split(";");
				for (int i = 0; i < listCoord.size(); i++)//遍历4个坐标
				{
					//再按,分割x和y
					QList<QString> listXY;
					listXY = listCoord[i].split(",");
					if (listXY.size() != 2)
					{
						continue;
					}
					else
					{
						if (0 == i)
						{
							m_pDevDataBeltRealign->m_pDevDataCam->m_BeltEdgePf1.setX(listXY[0].toDouble());
							m_pDevDataBeltRealign->m_pDevDataCam->m_BeltEdgePf1.setY(listXY[1].toDouble());
						}
						else if (1 == i)
						{
							m_pDevDataBeltRealign->m_pDevDataCam->m_BeltEdgePf2.setX(listXY[0].toDouble());
							m_pDevDataBeltRealign->m_pDevDataCam->m_BeltEdgePf2.setY(listXY[1].toDouble());
						}
						else if (2 == i)
						{
							m_pDevDataBeltRealign->m_pDevDataCam->m_BeltEdgePf3.setX(listXY[0].toDouble());
							m_pDevDataBeltRealign->m_pDevDataCam->m_BeltEdgePf3.setY(listXY[1].toDouble());
						}
						else if (3 == i)
						{
							m_pDevDataBeltRealign->m_pDevDataCam->m_BeltEdgePf4.setX(listXY[0].toDouble());
							m_pDevDataBeltRealign->m_pDevDataCam->m_BeltEdgePf4.setY(listXY[1].toDouble());
						}
					}
				}
			}
			m_pDevDataBeltRealign->m_pMutex->unlock();
		}
	}
}

void CWorkMng::handleCorrectData()
{
	// 检查指针有效性
	if (!m_pDevDataBeltRealign || m_bShouldStop)
	{
		return;
	}
	//判断是否需要进入跑偏报警判断
	bool bTem = false;
	if (m_pDevDataBeltRealign->m_nIsHaveBeltRunState == 0)//没有皮带运行状态，必须每次判断
	{
		bTem = true;
	}
	else if (m_pDevDataBeltRealign->m_nIsHaveBeltRunState == 1)//有皮带跑偏状态
	{
		if (m_pDevDataBeltRealign->m_bBeltWork)//皮带正在运行
		{
			bTem = true;
		}
		else
		{
			bTem = false;
		}
	}
	m_pDevDataBeltRealign->m_vecLeftValue.push_back(m_pDevDataBeltRealign->m_dLeftCurrVal);
	m_pDevDataBeltRealign->m_vecRightValue.push_back(m_pDevDataBeltRealign->m_dRightCurrVal);
	//m_pDevDataBeltRealign->m_pMutex->unlock();

	//跑偏报警
	if (bTem)
	{
		//m_pDevDataBeltRealign->m_pMutex->lock();
		if (m_pDevDataBeltRealign->m_bIsCorrect || m_pDevDataBeltRealign->m_bIsCorrectTwo)//发生了跑
		{
			if (m_pDevDataBeltRealign->m_nLogNum == m_pDevDataBeltRealign->m_nAlarmTime)//每10秒钟写一次
			{
				if (m_bIsRecord)//如果之前存在正常的记录，则需要闭环
				{
					closeResult();
				}
				if (!m_pDevDataBeltRealign->m_bIsRecord)//发生跑偏但是跑偏结果记录没有插入
				{
					QString sAlarmDesc = "";
					addAlarm(sAlarmDesc);
				}
				else//进入了跑偏报警，但是之前已经记录过了
				{
					//判断一下持续时间是否满30分钟，如果满30分钟就将这个记录闭环，并插入一条新的记录
					if (qAbs(QDateTime::fromString(m_pDevDataBeltRealign->m_sLastAlarmTime, "yyyy/MM/dd hh:mm:ss").secsTo(QDateTime::currentDateTime())) > m_pDevDataBeltRealign->m_nResultNum)//超过了
					{
						//首先闭环当前的报警
						closeAlarm();

						//然后插入新的报警
						QString sAlarmDesc = "";
						addAlarm(sAlarmDesc);
					}
				}
			}
			else
			{
				m_pDevDataBeltRealign->m_nLogNum++;
				m_pDevDataBeltRealign->m_nDisAlarmNum = 0;
			}
		}
		else
		{
			if (m_pDevDataBeltRealign->m_bIsRecord)//没有发生跑偏，且之前已经插入过跑偏记录，则要将其闭环
			{
				if (m_pDevDataBeltRealign->m_nDisAlarmNum == m_pDevDataBeltRealign->m_nAlarmTime)
				{
					closeAlarm();
				}
				else
				{
					m_pDevDataBeltRealign->m_nDisAlarmNum++;
				}
			}
			else
			{
				//正常情况下每隔一段时间需要插入一条新的记录
				if (m_nResultCount == m_pDevDataBeltRealign->m_nResultNum)
				{
					if (m_bIsRecord)
					{
						closeResult();
					}
					//插入新的记录
					addResult();
					m_nResultCount = 0;
				}
				else
				{
					m_nResultCount++;
				}
			}
		}
		//m_pDevDataBeltRealign->m_pMutex->unlock();
	}
	else//这里判断一下是否需要处理未闭环的记录
	{
		//m_pDevDataBeltRealign->m_pMutex->lock();
		if (m_pDevDataBeltRealign->m_bIsRecord)//皮带不运行，且之前已经插入过跑偏记录，则要将其闭环
		{
			closeAlarm();
		}

		//正常情况下每隔一段时间需要插入一条新的记录
		if (m_nResultCount == m_pDevDataBeltRealign->m_nResultNum)
		{
			if (m_bIsRecord)
			{
				closeResult();
			}
			//插入新的记录
			addResult();
			m_nResultCount = 0;
		}
		else
		{
			m_nResultCount++;
		}
		//m_pDevDataBeltRealign->m_pMutex->unlock();
	}
}

void CWorkMng::addAlarm(QString& sAlarmDesc)
{
	// 检查指针有效性
	if (!m_pDevDataBeltRealign || !m_pCamerCtrMange || m_bShouldStop) 
	{
		return;
	}
	QString sImgUrl = m_pDevDataBeltRealign->m_sRecordPath + "Image/";
	QString sHttpUrl = "";
	//首先抓拍一次跑偏图
	if (!m_pDevDataBeltRealign->m_pDevDataCam->m_bLogin)
	{
		SCtrInfo infoTx;
		infoTx.m_sGuid = m_pDevDataBeltRealign->m_sPointGuid;
		infoTx.m_nCtrCmd = enCLogin;
		infoTx.m_nCtrDevType = enSubDev1;
		infoTx.m_nSubCtrDevType = enCamer;
		infoTx.m_bCtrSync = true;
		bool bTxRet = m_pCamerCtrMange->CtrDevice(infoTx);
	}
	SCtrInfo infoTx;
	infoTx.m_sGuid = m_pDevDataBeltRealign->m_sPointGuid;
	infoTx.m_nCtrCmd = enCGetImage;
	infoTx.m_nCtrDevType = enSubDev1;
	infoTx.m_nSubCtrDevType = enCamer;
	infoTx.m_bCtrSync = true;
	infoTx.m_sImgUrl = sImgUrl;
	bool bTxRet = m_pCamerCtrMange->CtrDevice(infoTx);
	if (!bTxRet)
	{
		//生成日志
		SExecLog sDevExecLog;
		sDevExecLog.m_sDevName = m_pDevDataBeltRealign->m_pDevDataCam->m_sCamerName;
		sDevExecLog.m_sFuncName = A2T("跑偏算法");
		sDevExecLog.m_sDescriptionl = A2T("跑偏图抓拍失败") + infoTx.m_sReData;
		sDevExecLog.m_nAlarmLevel = enSysAlarm;
		CLogOper::instance().addMsg(sDevExecLog);
	}
	sImgUrl = infoTx.m_sImgUrl;
	sHttpUrl = g_sHttpUrl + infoTx.m_sImgUrl.remove(m_pDevDataBeltRealign->m_sRecordPath);
	//数据库中插入跑偏结果
	QString sGuid = createGuid();
	QString sErrMsg = "";
	if (m_pDevDataBeltRealign->m_bIsCorrect)
	{
		sAlarmDesc = A2T("%1,检测到皮带一级跑偏").arg(m_pDevDataBeltRealign->m_sPointName);
	}
	else if (m_pDevDataBeltRealign->m_bIsCorrectTwo)
	{
		sAlarmDesc = A2T("%1,检测到皮带二级跑偏").arg(m_pDevDataBeltRealign->m_sPointName);
	}
	bool bRet = CSqlOper::instance().addCorrectResult(sGuid, m_pDevDataBeltRealign->m_sPointGuid, m_pDevDataBeltRealign->m_sPointName, m_pDevDataBeltRealign->m_dLeftCurrVal * 10, m_pDevDataBeltRealign->m_dRightCurrVal * 10
		, QDateTime::currentDateTime(), sImgUrl, sHttpUrl, sAlarmDesc, m_pDevDataBeltRealign->m_sBeltName, 1, m_pDevDataBeltRealign->m_nRealignSetValue * 10, m_pDevDataBeltRealign->m_nRealignSetValueTwo * 10, sErrMsg);
	if (!bRet)
	{
		//生成日志
		SExecLog sDevExecLog;
		sDevExecLog.m_sDevName = m_pDevDataBeltRealign->m_pDevDataCam->m_sCamerName;
		sDevExecLog.m_sFuncName = A2T("跑偏算法");
		sDevExecLog.m_sDescriptionl = A2T("插入跑偏结果失败");
		sDevExecLog.m_nAlarmLevel = enSysAlarm;
		CLogOper::instance().addMsg(sDevExecLog);
	}
	else
	{
		m_pDevDataBeltRealign->m_sResultGuid = sGuid;
		m_pDevDataBeltRealign->m_bIsRecord = true;
		m_pDevDataBeltRealign->m_sLastAlarmTime = QDateTime::currentDateTime().toString("yyyy/MM/dd hh:mm:ss");
	}
	//生成日志
	SExecLog sDevExecLog;
	sDevExecLog.m_sDevName = m_pDevDataBeltRealign->m_pDevDataCam->m_sCamerName;
	sDevExecLog.m_sFuncName = A2T("跑偏算法");
	sDevExecLog.m_sDescriptionl = sAlarmDesc;
	sDevExecLog.m_nAlarmLevel = enProcessAlarm;
	CLogOper::instance().addMsg(sDevExecLog);
	m_pDevDataBeltRealign->m_nLogNum = 0;
	m_pDevDataBeltRealign->m_nDisAlarmNum = 0;
	m_nResultCount = 0;
}

void CWorkMng::closeAlarm()
{
	//计算平均值
	double dSumLeft = 0.0;
	double dSumRight = 0.0;
	double dAvgLeft = 0.0;
	double dAvgRight = 0.0;
	if (m_pDevDataBeltRealign->m_vecLeftValue.size() > 0)
	{
		for (double value : m_pDevDataBeltRealign->m_vecLeftValue)
		{
			dSumLeft += value;
		}
		dAvgLeft = dSumLeft / m_pDevDataBeltRealign->m_vecLeftValue.size();
	}
	else
	{
		dAvgLeft = 0;
	}
	m_pDevDataBeltRealign->m_vecLeftValue.clear();
	if (m_pDevDataBeltRealign->m_vecRightValue.size() > 0)
	{
		for (double value : m_pDevDataBeltRealign->m_vecRightValue)
		{
			dSumRight += value;
		}
		dAvgRight = dSumRight / m_pDevDataBeltRealign->m_vecRightValue.size();
	}
	else
	{
		dAvgRight = 0;
	}
	m_pDevDataBeltRealign->m_vecRightValue.clear();
	QString sErrMsg = "";
	bool bRet = CSqlOper::instance().updateCorrectStopTime(m_pDevDataBeltRealign->m_sResultGuid, dAvgLeft * 10, dAvgRight * 10, sErrMsg);
	if (!bRet)
	{
		//生成日志
		SExecLog sDevExecLog;
		sDevExecLog.m_sDevName = m_pDevDataBeltRealign->m_pDevDataCam->m_sCamerName;
		sDevExecLog.m_sFuncName = A2T("跑偏算法");
		sDevExecLog.m_sDescriptionl = A2T("更新跑偏结束时间失败");
		sDevExecLog.m_nAlarmLevel = enSysAlarm;
		CLogOper::instance().addMsg(sDevExecLog);
	}
	else
	{
		m_pDevDataBeltRealign->m_bIsRecord = false;
		m_pDevDataBeltRealign->m_sResultGuid = "";
		m_pDevDataBeltRealign->m_sLastAlarmTime = "";
	}
}

void CWorkMng::addResult()
{
	QString sImgUrl = m_pDevDataBeltRealign->m_sRecordPath + "Image/";
	QString sHttpUrl = "";
	//首先抓拍一次跑偏图
	if (!m_pDevDataBeltRealign->m_pDevDataCam->m_bLogin)
	{
		SCtrInfo infoTx;
		infoTx.m_sGuid = m_pDevDataBeltRealign->m_sPointGuid;
		infoTx.m_nCtrCmd = enCLogin;
		infoTx.m_nCtrDevType = enSubDev1;
		infoTx.m_nSubCtrDevType = enCamer;
		infoTx.m_bCtrSync = true;
		bool bTxRet = m_pCamerCtrMange->CtrDevice(infoTx);
	}
	SCtrInfo infoTx;
	infoTx.m_sGuid = m_pDevDataBeltRealign->m_sPointGuid;
	infoTx.m_nCtrCmd = enCGetImage;
	infoTx.m_nCtrDevType = enSubDev1;
	infoTx.m_nSubCtrDevType = enCamer;
	infoTx.m_bCtrSync = true;
	infoTx.m_sImgUrl = sImgUrl;
	bool bTxRet = m_pCamerCtrMange->CtrDevice(infoTx);
	if (!bTxRet)
	{
		//生成日志
		SExecLog sDevExecLog;
		sDevExecLog.m_sDevName = m_pDevDataBeltRealign->m_pDevDataCam->m_sCamerName;
		sDevExecLog.m_sFuncName = A2T("跑偏算法");
		sDevExecLog.m_sDescriptionl = A2T("跑偏图抓拍失败") + infoTx.m_sReData;
		sDevExecLog.m_nAlarmLevel = enSysAlarm;
		CLogOper::instance().addMsg(sDevExecLog);
	}
	sImgUrl = infoTx.m_sImgUrl;
	sHttpUrl = g_sHttpUrl + infoTx.m_sImgUrl.remove(m_pDevDataBeltRealign->m_sRecordPath);
	//数据库中插入跑偏结果
	QString sGuid = createGuid();
	QString sErrMsg = "";
	QString sAlarmDesc = "";
	int nAlarm = 0;
	if (m_pDevDataBeltRealign->m_bIsCorrect)
	{
		sAlarmDesc = A2T("%1,检测到皮带一级跑偏").arg(m_pDevDataBeltRealign->m_sPointName);
		nAlarm = 1;
	}
	else if (m_pDevDataBeltRealign->m_bIsCorrectTwo)
	{
		sAlarmDesc = A2T("%1,检测到皮带二级跑偏").arg(m_pDevDataBeltRealign->m_sPointName);
		nAlarm = 1;
	}
	else
	{
		sAlarmDesc = A2T("未检测到皮带跑偏");
		nAlarm = 0;
	}
	bool bRet = CSqlOper::instance().addCorrectResult(sGuid, m_pDevDataBeltRealign->m_sPointGuid, m_pDevDataBeltRealign->m_sPointName, m_pDevDataBeltRealign->m_dLeftCurrVal * 10, m_pDevDataBeltRealign->m_dRightCurrVal * 10
		, QDateTime::currentDateTime(), sImgUrl, sHttpUrl, sAlarmDesc, m_pDevDataBeltRealign->m_sBeltName, nAlarm, m_pDevDataBeltRealign->m_nRealignSetValue * 10, m_pDevDataBeltRealign->m_nRealignSetValueTwo * 10, sErrMsg);
	if (!bRet)
	{
		//生成日志
		SExecLog sDevExecLog;
		sDevExecLog.m_sDevName = m_pDevDataBeltRealign->m_pDevDataCam->m_sCamerName;
		sDevExecLog.m_sFuncName = A2T("跑偏算法");
		sDevExecLog.m_sDescriptionl = A2T("插入跑偏结果失败");
		sDevExecLog.m_nAlarmLevel = enSysAlarm;
		CLogOper::instance().addMsg(sDevExecLog);
	}
	else
	{
		m_sLastGuid = sGuid;
		m_bIsRecord = true;
	}
}

void CWorkMng::closeResult()
{
	//计算平均值
	double dSumLeft = 0.0;
	double dSumRight = 0.0;
	double dAvgLeft = 0.0;
	double dAvgRight = 0.0;
	if (m_pDevDataBeltRealign->m_vecLeftValue.size() > 0)
	{
		for (double value : m_pDevDataBeltRealign->m_vecLeftValue)
		{
			dSumLeft += value;
		}
		dAvgLeft = dSumLeft / m_pDevDataBeltRealign->m_vecLeftValue.size();
	}
	else
	{
		dAvgLeft = 0;
	}
	m_pDevDataBeltRealign->m_vecLeftValue.clear();
	if (m_pDevDataBeltRealign->m_vecRightValue.size() > 0)
	{
		for (double value : m_pDevDataBeltRealign->m_vecRightValue)
		{
			dSumRight += value;
		}
		dAvgRight = dSumRight / m_pDevDataBeltRealign->m_vecRightValue.size();
	}
	else
	{
		dAvgRight = 0;
	}
	m_pDevDataBeltRealign->m_vecRightValue.clear();
	QString sErrMsg = "";
	bool bRet = CSqlOper::instance().updateCorrectStopTime(m_sLastGuid, dAvgLeft * 10, dAvgRight * 10, sErrMsg);
	if (!bRet)
	{
		//生成日志
		SExecLog sDevExecLog;
		sDevExecLog.m_sDevName = m_pDevDataBeltRealign->m_pDevDataCam->m_sCamerName;
		sDevExecLog.m_sFuncName = A2T("跑偏算法");
		sDevExecLog.m_sDescriptionl = A2T("更新跑偏结束时间失败");
		sDevExecLog.m_nAlarmLevel = enSysAlarm;
		CLogOper::instance().addMsg(sDevExecLog);
	}
	else
	{
		m_sLastGuid = "";
		m_bIsRecord = false;
	}
}

void CWorkMng::run()
{
	while (!m_bShouldStop)
	{
		// 使用局部作用域管理锁，避免长时间持有锁
		{
			QMutexLocker locker(m_pMutex);

			// 检查停止标志和指针有效性
			if (!m_bhread.load() || m_bShouldStop || !m_pDevDataBeltRealign || !m_pCamerCtrMange) 
			{
				sysSleep(10);
				continue;
			}

			if (m_nLoginCount == 10)
			{
				if (m_pDevDataBeltRealign->m_pDevDataCam->m_bLogin == false)
				{
					SCtrInfo infoTx;
					infoTx.m_sGuid = m_pDevDataBeltRealign->m_pDevDataCam->m_sDevGuid;
					infoTx.m_nCtrCmd = enCLogin;
					infoTx.m_nCtrDevType = enSubDev1;
					infoTx.m_nSubCtrDevType = enCamer;
					infoTx.m_bCtrSync = false;
					bool bTxRet = m_pCamerCtrMange->CtrDevice(infoTx);
				}
				m_nLoginCount = 0;
			}
			else
			{
				m_nLoginCount++;
			}

			updateBeltCorrectState();
			handleCorrectData();
		}

		// 在锁外执行事件循环，避免阻塞其他线程
		if (!m_bShouldStop)
		{
			QEventLoop loop;
			QTimer::singleShot(1000, &loop, SLOT(quit()));
			loop.exec();
		}
	}
}
